class ExpensesController < ApplicationController
  before_filter :find_project

  def new
    @expense = @project.expenses.create(params[:expense])
    flash[:notice] = "Thank you for adding new expense item"
    respond_to do |format|
      format.html 
      format.js
    end
    #    sleep 4
  end
  
  def destroy
    @delete_success = true
    begin
      Expense.destroy(params[:id])
      flash[:notice] = "Expense item deleted successfuly"
    rescue Exception => exception
      #Log the exception that throwing it console.
      @delete_success = false
    end
  end

  def edit
    @expense = Expense.find(params[:id])
  end

  def cancel
    edit
  end

  def update
    #puts "testsssing -------" , params[:id]
    @expense = Expense.find(params[:id])
    @expense.update_attributes(
      :description => params[:expense][:description],
      :amount => params[:expense][:amount])
    flash[:notice] = "Expense item updated successfully"
  end

  private
  def find_project
    @project = Project.find(params[:project])
  end
end